//
//  ReportGenVC.m
//  Wallets Manager
//
//  Created by Nguyen Chi Cong on 11/27/12.
//  Copyright (c) 2012 BKHN. All rights reserved.
//

#import "ReportGenVC.h"

@interface ReportGenVC ()
{
    Wallet *w;
}

@end

@implementation ReportGenVC
@synthesize viewContainer;
@synthesize lbTotalIncome;
@synthesize lbMaxNameIncome;
@synthesize lbMaxAmountIncome;
@synthesize lbMaxDateIncome;
@synthesize lbTotalExpense;
@synthesize lbMaxNameExpense;
@synthesize lbMaxAmountExpense;
@synthesize lbMaxDateExpense;
@synthesize lbTotalLoan;
@synthesize lbMaxNameLoan;
@synthesize lbMaxAmountLoan;
@synthesize lbMaxDateLoan;
@synthesize lbTotalDebt;
@synthesize lbMaxNameDebt;
@synthesize lbMaxAmountDebt;
@synthesize lbMaxDateDebt;
@synthesize lbNameWallet;
@synthesize lbAmountWallet;
@synthesize scrContainer;
@synthesize logoIncome;
@synthesize logoExpense;
@synthesize logoLoan;
@synthesize logoDebt;
@synthesize logoWallet;
@synthesize lbDateReport;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    fromDate = [NSDate firstDayOfCurrentMonth];
    toDate   = [NSDate firstDayOfNextMonth];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [scrContainer setScrollEnabled:YES];
    scrContainer.contentSize = viewContainer.bounds.size;
    [scrContainer addSubview:viewContainer];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTimeMode:) name:@"ChangeTimeMode" object:nil];
    dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"MM/dd/yyyy"];
    
    w = ((folyAppDelegate *)[UIApplication sharedApplication].delegate).currentWallet;
    lbNameWallet.text = w.w_name;
    lbAmountWallet.text = [NSString stringWithFormat:@"%@ %@", w.w_balance, w.wToCurrency.c_symbol];
    logoWallet.image = [UIImage imageNamed:w.w_image];
}

-(void)viewWillAppear:(BOOL)animated
{
    [self reloadContentByDate];
    [super viewWillAppear:YES];
}

- (void)viewDidUnload
{
    [self setViewContainer:nil];
    [self setLbTotalIncome:nil];
    [self setLbMaxNameIncome:nil];
    [self setLbMaxAmountIncome:nil];
    [self setLbMaxDateIncome:nil];
    [self setLbTotalExpense:nil];
    [self setLbMaxNameExpense:nil];
    [self setLbMaxAmountExpense:nil];
    [self setLbMaxDateExpense:nil];
    [self setLbTotalLoan:nil];
    [self setLbMaxNameLoan:nil];
    [self setLbMaxAmountLoan:nil];
    [self setLbMaxDateLoan:nil];
    [self setLbTotalDebt:nil];
    [self setLbMaxNameDebt:nil];
    [self setLbMaxAmountDebt:nil];
    [self setLbMaxDateDebt:nil];
    [self setLbNameWallet:nil];
    [self setLbAmountWallet:nil];
    [self setScrContainer:nil];
    [self setLogoIncome:nil];
    [self setLogoExpense:nil];
    [self setLogoLoan:nil];
    [self setLogoDebt:nil];
    [self setLogoWallet:nil];
    [self setLbDateReport:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    fromDate            = nil;
    toDate              = nil;
    changTimeModeVC     = nil;
    dateFormatter       = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ChangeTimeMode" object:nil];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Notification Center Delegate
-(void)changeTimeMode:(NSNotification *)notification
{
    NSLog(@"OBJECT = %@", notification.object);
    
    fromDate = [notification.userInfo valueForKey:@"fromDate"];
    toDate = [notification.userInfo valueForKey:@"toDate"];
    NSLog(@"FROM = %@", [dateFormatter stringFromDate:fromDate]);
    NSLog(@"TO = %@", [dateFormatter stringFromDate:toDate]);
    
    [self reloadContentByDate];
    
}

- (IBAction)changeTime:(id)sender {
    
    if(!changTimeModeVC)
    {
        changTimeModeVC = [[ChangeTimeVC alloc] initWithNibName:@"ChangeTimeVC" bundle:nil ];
        [changTimeModeVC.view setFrame:CGRectMake(0, 480, 320, 460)];
        
        [gTabBarController.view addSubview:changTimeModeVC.view];
    }
    
    [UIView beginAnimations:@"push_animation" context:NULL];
    [UIView setAnimationDuration:0.1];
    [UIView setAnimationBeginsFromCurrentState:YES];
    
    [changTimeModeVC.view setFrame:CGRectMake(0, 20, 320, 460)];
    
    [UIView commitAnimations];
}

-(IBAction)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)reloadContentByDate
{
    //UPDATE title
    NSDate *tempDate;
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:toDate];
    NSInteger day = [components day];
    day--;
    [components setDay:day];
    tempDate = [gregorianCalendar dateFromComponents:components];

    lbDateReport.text = [NSString stringWithFormat:@"%@ - %@", [dateFormatter stringFromDate:fromDate], [dateFormatter stringFromDate:tempDate]];
    
    //Update income
    [self loadIncome];
    
    //UpdateExpense
    [self loadExpense];
    
    //Update Loan
    [self loadLoan];
    
    
    //Update Debt
    [self loadDebt];
    
    
    
}

-(void) loadIncome
{
    NSDecimalNumber *totalIncome = [DataBaseAccess numberSumIncomesinWallet:w inTimeFrom:fromDate to:toDate];
    Income *maxIncome = [DataBaseAccess maxIncomeInWallet:w inTimeFrom:fromDate to:toDate];
    
    lbTotalIncome.text = [NSString stringWithFormat:@"%@ %@", totalIncome, w.wToCurrency.c_symbol];
    
    if(!maxIncome)
    {
        lbMaxNameIncome.text = [NSString stringWithFormat:@"No income"];
        lbMaxAmountIncome.text = [NSString stringWithFormat:@"%i %@",0, w.wToCurrency.c_symbol];
        lbMaxDateIncome.text = [NSString stringWithFormat:@"No data"];
        return;
    }
    
    lbMaxNameIncome.text = [NSString stringWithFormat:@"%@", maxIncome.i_name];
    lbMaxAmountIncome.text = [NSString stringWithFormat:@"%@ %@",maxIncome.i_amount, w.wToCurrency.c_symbol];
    lbMaxDateIncome.text = [NSString stringWithFormat:@"%@", [dateFormatter stringFromDate:maxIncome.i_date]];
}

-(void) loadExpense
{
    NSDecimalNumber *totalExpense = [DataBaseAccess numberSumExpensesinWallet:w inTimeFrom:fromDate to:toDate];
    Expense *maxExpense = [DataBaseAccess maxExpenseInWallet:w inTimeFrom:fromDate to:toDate];
    
    lbTotalExpense.text = [NSString stringWithFormat:@"%@ %@", totalExpense, w.wToCurrency.c_symbol];
    
    if(!maxExpense)
    {
        lbMaxNameExpense.text = [NSString stringWithFormat:@"No expense"];
        lbMaxAmountExpense.text = [NSString stringWithFormat:@"%i %@",0, w.wToCurrency.c_symbol];
        lbMaxDateExpense.text = [NSString stringWithFormat:@"No data"];
        return;
    }
    
    lbMaxNameExpense.text = [NSString stringWithFormat:@"%@", maxExpense.e_name];
    lbMaxAmountExpense.text = [NSString stringWithFormat:@"%@ %@",maxExpense.e_amount, w.wToCurrency.c_symbol];
    lbMaxDateExpense.text = [NSString stringWithFormat:@"%@", [dateFormatter stringFromDate:maxExpense.e_date]];
}

-(void)loadLoan
{
    NSDecimalNumber *totalLoan = [DataBaseAccess numberSumLoansinWallet:w inTimeFrom:fromDate to:toDate];
    Loan *maxLoan = [DataBaseAccess maxLoanEventsInWallet:w inTimeFrom:fromDate to:toDate];
    
    lbTotalLoan.text = [NSString stringWithFormat:@"%@ %@", totalLoan, w.wToCurrency.c_symbol];
    
    if(!maxLoan)
    {
        lbMaxNameLoan.text = [NSString stringWithFormat:@"No loan"];
        lbMaxAmountLoan.text = [NSString stringWithFormat:@"%i %@",0, w.wToCurrency.c_symbol];
        lbMaxDateLoan.text = [NSString stringWithFormat:@"No data"];
        return;
    }
    
    lbMaxNameLoan.text = [NSString stringWithFormat:@"%@", maxLoan.l_name];
    lbMaxAmountLoan.text = [NSString stringWithFormat:@"%@ %@",maxLoan.l_amount, w.wToCurrency.c_symbol];
    lbMaxDateLoan.text = [NSString stringWithFormat:@"%@", [dateFormatter stringFromDate:maxLoan.l_date]];
}

-(void)loadDebt
{
    NSDecimalNumber *totalDebt = [DataBaseAccess numberSumDebtsinWallet:w inTimeFrom:fromDate to:toDate];
    Debt *maxDebt = [DataBaseAccess maxDebtEventsInWallet:w inTimeFrom:fromDate to:toDate];
    
    lbTotalDebt.text = [NSString stringWithFormat:@"%@ %@", totalDebt, w.wToCurrency.c_symbol];
    
    if(!maxDebt)
    {
        lbMaxNameDebt.text = [NSString stringWithFormat:@"No debt"];
        lbMaxAmountDebt.text = [NSString stringWithFormat:@"%i %@",0, w.wToCurrency.c_symbol];
        lbMaxDateDebt.text = [NSString stringWithFormat:@"No data"];
        return;
    }
    
    lbMaxNameDebt.text = [NSString stringWithFormat:@"%@", maxDebt.d_name];
    lbMaxAmountDebt.text = [NSString stringWithFormat:@"%@ %@",maxDebt.d_amount, w.wToCurrency.c_symbol];
    lbMaxDateDebt.text = [NSString stringWithFormat:@"%@", [dateFormatter stringFromDate:maxDebt.d_date]];
}




@end
